package com.ruoyi.common.service.impl;

import com.ruoyi.common.config.RuoYiConfig;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

@Service
public class ResourceReadService {


    public String readFileByResource(String fileName) throws IOException {
        // 构建本地文件资源
        Resource resource = new FileSystemResource(RuoYiConfig.getPathVideo() + fileName);

        // 校验资源是否存在
        if (!resource.exists() || !resource.isFile()) {
            throw new RuntimeException("文件不存在：" + fileName);
        }

        // 读取资源流
        try (InputStream is = resource.getInputStream()) {
            byte[] data = is.readAllBytes(); // JDK 9+ 支持，或用 IOUtils 工具类
            return new String(data, StandardCharsets.UTF_8);
        }
    }
}
